package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.HostBean;
import com.luoxiang.socialandroid.utils.CustomClickListener;

import java.util.List;

public class TuijianRecyAdapter extends RecyclerView.Adapter<TuijianRecyAdapter.ViewHolder> {
    private List<HostBean> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    public void setData(List<HostBean> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int userid, int covertype, String name, String icon, String cover,String videoPath); //传递boolean类型数据给activity
    }

    TuijianRecyAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(TuijianRecyAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public TuijianRecyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tuijian, parent, false);
        TuijianRecyAdapter.ViewHolder holder = new TuijianRecyAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        HostBean hostBean = list.get(position);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.icon_error_img2)//图片加载出来前，显示的图片
                .fallback(R.mipmap.icon_error_img2) //url为空的时候,显示的图片
                .error(R.mipmap.icon_error_img2);//图片加载失败后，显示的图片

        RoundedCorners roundedCorners = new RoundedCorners(30);
        RequestOptions options1 = RequestOptions.bitmapTransform(roundedCorners);
        if (hostBean.getOnline() == 2) {
            holder.im1_tuijian.setImageResource(R.mipmap.icon_zailiao);
            holder.im1_tuijian.setVisibility(View.VISIBLE);
        } else if (hostBean.getOnline() == 3) {
            holder.im1_tuijian.setImageResource(R.mipmap.icon_online);
            holder.im1_tuijian.setVisibility(View.VISIBLE);
        } else {
            holder.im1_tuijian.setVisibility(View.GONE);
        }
        holder.icon_txname_bg.bringToFront();
        holder.tx2_tuijian.setText(hostBean.getName());
        holder.tx2_tuijian.bringToFront();
        if (hostBean.getCoverType() == 0) {
            //设置图片
            Glide.with(MyAppliaction.getContext())
                    .load(url + hostBean.getIcon())
                    .transform(new CenterCrop(), new RoundedCorners(30))
                    .into(holder.im_tuijian);
            holder.img_isshipin.setVisibility(View.GONE);

//            ImageView imageView = (ImageView) holder.im_tuijian;
//            imageView.setScaleType(ImageView.ScaleType.CENTER);
        } else {
            Glide.with(MyAppliaction.getContext())
                    .load(url + hostBean.getCover())
                    .transform(new CenterCrop(), new RoundedCorners(30))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(holder.im_tuijian);
            holder.img_isshipin.setVisibility(View.GONE);
//            LogUtils.i("shipin",url + hostBean.getCover() + "x-oss-process=video/snapshot,t_1,f_png,w_0,h_0,ar_auto");
        }
        holder.itemView.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onItemAddClick.onItemClicks(position, hostBean.getUserId(), hostBean.getCoverType(), hostBean.getName(), hostBean.getIcon(), hostBean.getCover(),hostBean.getVideoPath());
            }

            @Override
            protected void onFastClick() {
                return;
            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im_tuijian, img_isshipin, im1_tuijian, icon_txname_bg;
        private TextView tx2_tuijian;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_txname_bg = itemView.findViewById(R.id.icon_txname_bg);
            im_tuijian = itemView.findViewById(R.id.im_tuijian);
            im1_tuijian = itemView.findViewById(R.id.im1_tuijian);
            tx2_tuijian = itemView.findViewById(R.id.tx2_tuijian);
            img_isshipin = itemView.findViewById(R.id.img_isshipin);
        }
    }
}
